﻿using System.ComponentModel;
using System.IO;
using System.Runtime.CompilerServices;
using System.Windows;
using GemGui.Annotations;

namespace GemGui.ViewModel
{
    /// <summary>
    /// Serves as a view model for the configuration dialog, and for passing that information.
    /// </summary>
    public class ConfigurationViewModel : INotifyPropertyChanged
    {
        private int m_maxSearchDepth;
        private string m_rootFolderForSearch;

        /// <summary>
        /// Gets or sets the configured root folder for search.
        /// </summary>
        public string RootFolderForSearch {
            get
            {
                return m_rootFolderForSearch;
            }

            set
            {
                m_rootFolderForSearch = value;
           
                OnPropertyChanged();
                OnPropertyChanged("HasRootFolder");
                OnPropertyChanged("IsRootFolderValid");
            }
        }

        /// <summary>
        /// Gets or sets the maximum folder depth for SCC search.
        /// </summary>
        public int MaxSearchDepth
        {
            get
            {
                return m_maxSearchDepth;
            }

            set
            {
                m_maxSearchDepth = value;
           
                OnPropertyChanged();
            }
        }

        public bool HasRootFolder
        {
            get
            {
                if (string.IsNullOrEmpty(RootFolderForSearch))
                {
                    return false;
                }

                if (!Directory.Exists(RootFolderForSearch))
                {
                    return false;
                }

                return true;
            }
        }

        public Visibility IsRootFolderValid
        {
            get
            {
                if (string.IsNullOrEmpty(RootFolderForSearch) ||
                    HasRootFolder)
                {
                    return Visibility.Hidden;
                }
                
                return Visibility.Visible;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
